package com.liu.yubi.controller;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author lqh
 * @since 2025-06-02 23:08
 * 线程池控制器
 */
@RequestMapping("/queue")
@RestController
@Slf4j
public class QueueController {

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;


    @GetMapping("/add")
    public void addQueue(String name) {
        CompletableFuture.runAsync(() -> {
            log.info ("任务执行中："+name+",执行线程是:"+Thread.currentThread ().getName ());
            try {
                Thread.sleep (600000);
            } catch (InterruptedException e) {
                e.printStackTrace ();
            }
        }, threadPoolExecutor);

    }
    @GetMapping("/get")
    public String getProperty(){
        LinkedHashMap<String, Object> property = new LinkedHashMap<> ();
        property.put ("队列长度",threadPoolExecutor.getQueue ().size ());
        property.put ("任务总数",threadPoolExecutor.getTaskCount ());
        property.put ("已完成任务数",threadPoolExecutor.getCompletedTaskCount ());
        property.put ("正在工作的线程数",threadPoolExecutor.getActiveCount ());
        return JSONUtil.toJsonStr (property);
    }
}
